﻿using System;
using System.Collections.Generic;
using System.Data.Entity.Validation;
using System.Diagnostics;
using System.Linq;
using System.Runtime.Remoting.Contexts;
using System.Text;
using System.Threading.Tasks;
using System.Collections;

namespace DBModel
{

    public partial class WorkAdvertisement
    {
        public override string ToString()
        {
            return WorkTitle;
        }
    }

    public class WorkAdvertisementDB
    {
        public static List<WorkAdvertisement> GetAllWorkAdvertisement()
        {
            try
            {
                return DB.Context.WorkAdvertisements.Where(w => w.IsDeleted == false).ToList();
            }

            catch (DbEntityValidationException dbEx)
            {
                foreach (var validationErrors in dbEx.EntityValidationErrors)
                {
                    foreach (var validationError in validationErrors.ValidationErrors)
                    {
                        var error = string.Format("Property: {0} Error: {1}", validationError.PropertyName,
                            validationError.ErrorMessage);
                        Trace.TraceInformation(error);
                        throw new ApplicationException(error);
                    }
                }
            }
            return null;
        }

        public static List<WorkAdvertisement> GetWorkAdvertisementsByPublisher(int userId)
        {
            var adverts = DB.Context.WorkAdvertisements.Where(w => w.PublisherUserId == userId).ToList();
            var advertsNotDeletes = adverts.Where(a => a.IsDeleted == false).ToList();
            return advertsNotDeletes;
        }

        public static WorkAdvertisement GetWorkAdvertisementById(int workId)
        {
            return DB.Context.WorkAdvertisements.SingleOrDefault(w => w.WorkAdvertisementId == workId);
        }

        public static void Delete(int id)
        {
            var advert = DB.Context.WorkAdvertisements.SingleOrDefault(w => w.WorkAdvertisementId == id);
            if (advert != null)
                advert.IsDeleted = true;
        }

        public static void Restore(int id)
        {
            var advert = DB.Context.WorkAdvertisements.SingleOrDefault(w => w.WorkAdvertisementId == id);
            if (advert != null)
                advert.IsDeleted = false;
        }

        public static List<WorkAdvertisement> GetAllDeletedByUserId(int userId)
        {
            var adverts = DB.Context.WorkAdvertisements.Where(w => w.PublisherUserId == userId).ToList();
            var advertsDeleted = adverts.Where(a => a.IsDeleted).ToList();
            return advertsDeleted;
        }

        public static List<WorkAdvertisement> GetAllOutdatedWorkAdvertisements()
        {
            return DB.Context.WorkAdvertisements.Where(w => !w.IsDeleted)
                .Where(w => !w.IsDeleted &&
                        ((DateTime.Now - w.PublishedDate).Days > 20 ||
                        (DateTime.Now - w.LastApplicationDate).Days > 14)).ToList();
        }

        public static void DeleteAllOutdatedWorkAdvertisement()
        {
            List<WorkAdvertisement> outdatedWorkAdvertisements = GetAllOutdatedWorkAdvertisements();
            foreach (WorkAdvertisement workAdvertisement in outdatedWorkAdvertisements)
            {
                workAdvertisement.IsDeleted = true;
            }
            DB.Context.SaveChanges();
        }
    }
}
